package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpClientUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 双色球历史中奖信息抓取
 * 数据来源：http://www.cwl.gov.cn/
 * 数据接口：http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice
 */
public class SsqHttp {
    private static final Logger logger = LoggerFactory.getLogger(SsqHttp.class);

    public static List<LinkedHashMap<String,Object>> ssqhistory(){
        List<LinkedHashMap<String,Object>> list=new ArrayList<>();
        LinkedHashMap<String,Object> map =new LinkedHashMap<>();
        String host="http://www.cwl.gov.cn/cwl_admin";
        String path="/front/cwlkj/search/kjxx/findDrawNotice";
        Map<String,String> headers= new HashMap<>();
        headers.put("Content-Type","application/json;charset=UTF-8");
        Map<String,String> query=new HashMap<>();
        query.put("name","ssq");
        query.put("issueCount","1");
        query.put("issueStart","");
        query.put("issueEnd","");
        query.put("dayStart","");
        query.put("dayEnd","");
        try {
           HttpResponse response= HttpClientUtils.doGet(host,path,null,headers,query);
           int state=response.getStatusLine().getStatusCode();
           String resp= EntityUtils.toString(response.getEntity());
           if (state !=200){
               logger.error("获取双色球请求错误：code={},错误信息:{}",state,resp);
           }else {
               JSONObject jsonObject=JSONObject.parseObject(resp);
               if (jsonObject.getInteger("state") == 0){
                   JSONArray array=JSONArray.parseArray(jsonObject.getString("result"));
                   JSONObject ssq=(JSONObject) array.get(0);
                   map.put("id",ssq.getString("code").substring(2,7));
                   String red=ssq.getString("red");
                   String[] reds=red.split(",");
                   map.put("one",reds[0]);
                   map.put("two",reds[1]);
                   map.put("three",reds[2]);
                   map.put("four",reds[3]);
                   map.put("five",reds[4]);
                   map.put("six",reds[5]);
                   map.put("seven",ssq.getString("blue"));
                   map.put("bonusPool",ssq.getString("poolmoney")); //奖金池金额
                   JSONArray prizegrades= JSONArray.parseArray(ssq.getString("prizegrades"));
                   JSONObject one=(JSONObject) prizegrades.get(0);
                   map.put("oneNotes",one.getString("typenum")); //一等奖投注数
                   map.put("oneBonus",one.getString("typemoney")); //一等奖投注金额
                   JSONObject two =(JSONObject) prizegrades.get(1);
                   map.put("twoNotes",two.getString("typenum")); //二等奖投注数
                   map.put("twoBonus",two.getString("typemoney"));//二等奖投注金额
                   map.put("totalBonus",ssq.getString("sales"));//总投注金额
                   String date=ssq.getString("date");
                   map.put("openDate",date.substring(0,10)); //开奖日期
                   list.add(map);
               }
           }
        }catch (Exception e){
            logger.error("抓取中国福彩网双色球出错，错误信息：", e);
        }
        return list;
    }

}
